package com.chuzhong.sf.gui;

import com.chuzhong.sf.api.admin_api;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author 初衷.
 * @Date 2020/10/18
 **/
public class Admin_allUser  extends JFrame{

    private static JTable table;// 声明表格
    private static JPanel jl_1;//用户主页的版面

    private Object [][]data=null;
    public Admin_allUser(String username, String edu, Integer id,String passwrod) throws IOException {

        Font font = new Font("宋体", Font.PLAIN, 20);//设置字体
        jl_1 = new JPanel();
        super.setTitle("所有用户列表");
        JButton jButton = new JButton("重置密码");

        int a = 0;
        List<Map> maps = admin_api.admin_queryuserallinfo();
        String[] titles = {"用户名","总流水", "总收益", "开户时间","重置密码","删除用户","用户管理"};
        data=new Object[maps.size()][titles.length]; //将数据遍历为所需的格式
        for (int i=0;i<maps.size();i++){
            for (int j=0;j<titles.length;j++){
                data[i][0]  = maps.get(i).get("username");
                data[i][1]  = maps.get(i).get("info_liushui");
                data[i][2]  = maps.get(i).get("info_shouyi");
                data[i][3] =  maps.get(i).get("add_time");

            }
        }

//        table = new JTable(data, titles);
        DefaultTableModel defaultTableModel = new DefaultTableModel(data,titles);
        table = new JTable(defaultTableModel);
        table.getColumnModel().getColumn(3).setPreferredWidth(120);
        table.getColumnModel().getColumn(4).setCellRenderer(new MyButtonRender());
        //点击button 之后的逻辑
        MyEvent e = new MyEvent() {
            @Override
            public void invoke(ActionEvent e) {
                MyButton button = (MyButton)e.getSource();
                Integer integer = Integer.valueOf(button.getColumn());
                if(integer.equals(4)){
                    new new_password((String) maps.get(button.getRow()).get("username"), (Integer) maps.get(button.getRow()).get("id"));
                }else if(integer.equals(5)){

                    int n = JOptionPane.showConfirmDialog(null, "确定删除该用户账户及其下所有资料?", "删除用户",JOptionPane.YES_NO_OPTION);
                    System.out.println(n);
                    if(n==0){
                        defaultTableModel.removeRow(button.getRow());
                        try {
                            admin_api.admin_deluser((Integer) maps.get(button.getRow()).get("id"));
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }else if(integer.equals(6)){
                    try {
                        new Admin_User((String) maps.get(button.getRow()).get("username"),(String) maps.get(button.getRow()).get("edu"),(Integer) maps.get(button.getRow()).get("id"));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
                System.out.println("row:" + button.getRow() + "column :" + button.getColumn()+"text："+button.getText());
            }

        };
        MyButtonEditor editor = new MyButtonEditor(e);
        MyButtonEditor_del editor1 = new MyButtonEditor_del(e);
        MyButtonEditor_user editor2 = new MyButtonEditor_user(e);
        //设置表格的编辑器
        table.getColumnModel().getColumn(4).setCellEditor(editor);

        table.getColumnModel().getColumn(5).setCellRenderer(new MyButton_del());
        //设置表格的编辑器
        table.getColumnModel().getColumn(5).setCellEditor(editor1);

        table.getColumnModel().getColumn(6).setCellRenderer(new MyButton_user());
        //设置表格的编辑器
        table.getColumnModel().getColumn(6).setCellEditor(editor2);
        table.setRowHeight(25);
        table.setFont(font);
        /*头部添加一个按钮*/
        JPanel jPanel = new JPanel();
        JLabel jLabel = new JLabel("欢迎您！"+username+" 管理端");
        JButton jButton1 = new JButton("开设账户");
        jButton1.setFont(font);
        JButton jButton2 = new JButton("刷新页面");
        jButton2.setFont(font);
        jLabel.setFont(font);
        jPanel.add(jLabel);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.setFont(font);
        JTableHeader tableHeader = this.table.getTableHeader();
        tableHeader.setFont(font);
        add(jPanel,BorderLayout.NORTH);
//        add(jl_1,BorderLayout.SOUTH);
        add(new JScrollPane(table));
        setSize(1200, 700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        setLocation(300,100);
        setLocationRelativeTo(null);

        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Insert_user insert_user = new Insert_user();
            }
        };
        jButton1.addActionListener(actionListener);
        ActionListener actionListener1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                try {
                    new Admin_allUser(username, edu, id, passwrod);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        };
        jButton2.addActionListener(actionListener1);
    }



    public static void main(String[] args) throws IOException {
    }
}
